// pages/grade/list.js
var grade=require('./model/GradeModel.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight:200,
    currentGradeId:null,
    currentGradeText:null,
    gradeList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentGradeId:options.gradeId,
      currentGradeText: options.gradeText
    })
    var that=this
    var grades = wx.getStorageSync("grades")
    if(grades!=null&&grades!=undefined&&grades.length>0){
      that.setData({
        gradeList: grades,
        winHeight: grades.length * 40
      })
    }else{
      grade.getGradeList(function (list) {
        wx.setStorage({
          key: 'grades',
          data: list,
        })
        that.setData({
          gradeList: list,
          winHeight: list.length * 40
        })
      })
    }
    
  },
  clickItem:function(event){
    var gradeId = event.currentTarget.dataset.id
    var gradeText = event.currentTarget.dataset.text
    var that = this
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    if (that.data.currentGradeId != gradeId && that.data.currentGradeText != gradeText){
      prevPage.setData({
        _currentLevel: { id: gradeId, text: gradeText },
        _changeLevelState:true
      });
    }
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面

    // prevPage.setData({
    //   userName: that.data.contentStr
    // });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})